{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# 实战 Kaggle 比赛：预测房价\n",
    ":label:`sec_kaggle_house`\n",
    "\n",
    "现在我们已经介绍了一些建立和训练深度网络的基本工具，和网络正则化的技术（如权重衰减、Dropout等）。我们准备通过参加Kaggle比赛来将所有这些知识付诸实践。房价预测比赛是一个很好的起点。这个数据是相当通用的，不会需要使用带特殊结构的模型（就像音频或视频可能需要的那样）。此数据集由Bart de Cock于2011年收集 :cite:`De-Cock.2011` ，涵盖了2006-2010年期间亚利桑那州埃姆斯市的房价。它比哈里森和鲁宾菲尔德（1978年）的[波士顿房价](https://archive.ics.uci.edu/ml/machine-learning-databases/housing/housing.names)数据集要大得多，也有更多的特征。\n",
    "\n",
    "在本节中，我们将详细介绍数据预处理、模型设计和超参数选择。我们希望通过亲身实践的方式，你将获得一些直观的感受。这些感受将指导你数据科学家职业生涯。\n",
    "\n",
    "## 下载和缓存数据集\n",
    "\n",
    "在整本书中，我们将在各种下载的数据集上训练和测试模型。在这里，我们(**实现几个函数来方便下载数据**)。首先，我们维护字典`DATA_HUB`，其将数据集名称的字符串映射到数据集相关的二元组上，这个二元组包含数据集的url和验证文件完整性的sha-1密钥。所有这样的数据集都托管在地址为`DATA_URL`的站点上。\n"
   ],
   "metadata": {
    "origin_pos": 0
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "import hashlib\n",
    "import os\n",
    "import tarfile\n",
    "import zipfile\n",
    "import requests\n",
    "\n",
    "#@save\n",
    "DATA_HUB = dict()\n",
    "DATA_URL = 'http://d2l-data.s3-accelerate.amazonaws.com/'"
   ],
   "outputs": [],
   "metadata": {
    "origin_pos": 1,
    "tab": [
     "pytorch"
    ]
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "下面的`download`函数用来下载数据集，将数据集缓存在本地目录（默认情况下为`../data`）中，并返回下载文件的名称。如果缓存目录中已经存在此数据集文件，并且其sha-1与存储在`DATA_HUB`中的相匹配，我们将使用缓存的文件，以避免重复的下载。\n"
   ],
   "metadata": {
    "origin_pos": 2
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "def download(name, cache_dir=os.path.join('..', 'data')):  #@save\n",
    "    \"\"\"下载一个DATA_HUB中的文件，返回本地文件名。\"\"\"\n",
    "    assert name in DATA_HUB, f\"{name} 不存在于 {DATA_HUB}.\"\n",
    "    url, sha1_hash = DATA_HUB[name]\n",
    "    os.makedirs(cache_dir, exist_ok=True)\n",
    "    fname = os.path.join(cache_dir, url.split('/')[-1])\n",
    "    if os.path.exists(fname):\n",
    "        sha1 = hashlib.sha1()\n",
    "        with open(fname, 'rb') as f:\n",
    "            while True:\n",
    "                data = f.read(1048576)\n",
    "                if not data:\n",
    "                    break\n",
    "                sha1.update(data)\n",
    "        if sha1.hexdigest() == sha1_hash:\n",
    "            return fname  # Hit cache\n",
    "    print(f'正在从{url}下载{fname}...')\n",
    "    r = requests.get(url, stream=True, verify=True)\n",
    "    with open(fname, 'wb') as f:\n",
    "        f.write(r.content)\n",
    "    return fname"
   ],
   "outputs": [],
   "metadata": {
    "origin_pos": 3,
    "tab": [
     "pytorch"
    ]
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "我们还实现了两个额外的实用函数：一个是下载并解压缩一个zip或tar文件，另一个是将本书中使用的所有数据集从`DATA_HUB`下载到缓存目录中。\n"
   ],
   "metadata": {
    "origin_pos": 4
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "def download_extract(name, folder=None):  #@save\n",
    "    \"\"\"下载并解压zip/tar文件。\"\"\"\n",
    "    fname = download(name)\n",
    "    base_dir = os.path.dirname(fname)\n",
    "    data_dir, ext = os.path.splitext(fname)\n",
    "    if ext == '.zip':\n",
    "        fp = zipfile.ZipFile(fname, 'r')\n",
    "    elif ext in ('.tar', '.gz'):\n",
    "        fp = tarfile.open(fname, 'r')\n",
    "    else:\n",
    "        assert False, '只有zip/tar文件可以被解压缩。'\n",
    "    fp.extractall(base_dir)\n",
    "    return os.path.join(base_dir, folder) if folder else data_dir\n",
    "\n",
    "def download_all():  #@save\n",
    "    \"\"\"下载DATA_HUB中的所有文件。\"\"\"\n",
    "    for name in DATA_HUB:\n",
    "        download(name)"
   ],
   "outputs": [],
   "metadata": {
    "origin_pos": 5,
    "tab": [
     "pytorch"
    ]
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Kaggle\n",
    "\n",
    "[Kaggle](https://www.kaggle.com)是一个现在流行的举办机器学习比赛的平台。每场比赛都以一个数据集为中心。许多比赛都是由利益相关者赞助的，他们为获胜的解决方案提供奖金。该平台帮助用户通过论坛和共享代码进行互动，促进协作和竞争。虽然排行榜的追逐往往失控，研究人员短视地专注于预处理步骤，而不是考虑基础性问题，但一个客观的平台有巨大的价值。该平台促进了竞争方法之间的直接定量比较，以及代码共享。这便于每个人都可以了解哪些方法起作用，哪些没有起作用。如果你想参加Kaggle比赛，你首先需要注册一个账户（见 :numref:`fig_kaggle` ）。\n",
    "\n",
    "![Kaggle网站](../img/kaggle.png)\n",
    ":width:`400px`\n",
    ":label:`fig_kaggle`\n",
    "\n",
    "在房价预测比赛页面（如 :numref:`fig_house_pricing` 所示），你在\"Data\"选项卡下可以找到数据集。你可以通过下面的网址提交预测，并查看排名：\n",
    "\n",
    ">https://www.kaggle.com/c/house-prices-advanced-regression-techniques\n",
    "\n",
    "![房价预测比赛页面](../img/house-pricing.png)\n",
    ":width:`400px`\n",
    ":label:`fig_house_pricing`\n",
    "\n",
    "## 访问和读取数据集\n",
    "\n",
    "注意，竞赛数据分为训练集和测试集。每条记录都包括房屋的属性值和属性，如街道类型、施工年份、屋顶类型、地下室状况等。这些特征由各种数据类型组成。例如，建筑年份由整数表示，屋顶类型由离散类别表示，其他特征由浮点数表示。这就是现实让事情变得复杂的地方：例如，一些数据完全丢失了，缺失值被简单地标记为“NA”。每套房子的价格只出现在训练集中（毕竟这是一场比赛）。我们将希望划分训练集以创建验证集，但是在将预测结果上传到Kaggle之后，我们只能在官方测试集中评估我们的模型。在 :numref:`fig_house_pricing` 中，\"Data\"选项卡有下载数据的链接。\n",
    "\n",
    "开始之前，我们将[**使用`pandas`读入并处理数据**]，这是我们在 :numref:`sec_pandas` 中引入的。因此，在继续操作之前，您需要确保已安装`pandas`。幸运的是，如果你正在用Jupyter阅读该书，你可以在不离开笔记本的情况下安装`pandas`。\n"
   ],
   "metadata": {
    "origin_pos": 6
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "# 如果pandas没有被安装，请取消下一句的注释。\n",
    "# !pip install pandas\n",
    "\n",
    "%matplotlib inline\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import paddle\n",
    "from paddle import nn\n",
    "from d2l import paddle as d2l\n"
   ],
   "outputs": [],
   "metadata": {
    "origin_pos": 8,
    "tab": [
     "pytorch"
    ]
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "为方便起见，我们可以使用上面定义的脚本下载并缓存Kaggle房屋数据集。\n"
   ],
   "metadata": {
    "origin_pos": 10
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "DATA_HUB['kaggle_house_train'] = (  #@save\n",
    "    DATA_URL + 'kaggle_house_pred_train.csv',\n",
    "    '585e9cc93e70b39160e7921475f9bcd7d31219ce')\n",
    "\n",
    "DATA_HUB['kaggle_house_test'] = (  #@save\n",
    "    DATA_URL + 'kaggle_house_pred_test.csv',\n",
    "    'fa19780a7b011d9b009e8bff8e99922a8ee2eb90')"
   ],
   "outputs": [],
   "metadata": {
    "origin_pos": 11,
    "tab": [
     "pytorch"
    ]
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "我们使用`pandas`分别加载包含训练数据和测试数据的两个CSV文件。\n"
   ],
   "metadata": {
    "origin_pos": 12
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "train_data = pd.read_csv(download('kaggle_house_train'))\n",
    "test_data = pd.read_csv(download('kaggle_house_test'))"
   ],
   "outputs": [],
   "metadata": {
    "origin_pos": 13,
    "tab": [
     "pytorch"
    ]
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "训练数据集包括1460个样本，每个样本80个特征和1个标签，而测试数据包含1459个样本，每个样本80个特征。\n"
   ],
   "metadata": {
    "origin_pos": 14
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "print(train_data.shape)\n",
    "print(test_data.shape)"
   ],
   "outputs": [],
   "metadata": {
    "origin_pos": 15,
    "tab": [
     "pytorch"
    ]
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "让我们看看[**前四个和最后两个特征，以及相应标签**]（房价）。\n"
   ],
   "metadata": {
    "origin_pos": 16
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "print(train_data.iloc[0:4, [0, 1, 2, 3, -3, -2, -1]])"
   ],
   "outputs": [],
   "metadata": {
    "origin_pos": 17,
    "tab": [
     "pytorch"
    ]
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "我们可以看到，(**在每个样本中，第一个特征是ID，**)这有助于模型识别每个训练样本。虽然这很方便，但它不携带任何用于预测的信息。因此，在将数据提供给模型之前，(**我们将其从数据集中删除**)。\n"
   ],
   "metadata": {
    "origin_pos": 18
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "all_features = pd.concat((train_data.iloc[:, 1:-1], test_data.iloc[:, 1:]))"
   ],
   "outputs": [],
   "metadata": {
    "origin_pos": 19,
    "tab": [
     "pytorch"
    ]
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 数据预处理\n",
    "\n",
    "如上所述，我们有各种各样的数据类型。在开始建模之前，我们需要对数据进行预处理。让我们从数字特征开始。首先，我们应用启发式方法，[**将所有缺失的值替换为相应特征的平均值。**]然后，为了将所有特征放在一个共同的尺度上，我们(**通过将特征重新缩放到零均值和单位方差来标准化数据**)：\n",
    "\n",
    "$$x \\leftarrow \\frac{x - \\mu}{\\sigma}.$$\n",
    "\n",
    "要验证这确实转换了我们的特征（变量），使特征具有零均值和单位方差，即 $E[\\frac{x-\\mu}{\\sigma}] = \\frac{\\mu - \\mu}{\\sigma} = 0$和$E[(x-\\mu)^2] = (\\sigma^2 + \\mu^2) - 2\\mu^2+\\mu^2 = \\sigma^2$。直观地说，我们标准化数据有两个原因。首先，它方便优化。其次，因为我们不知道哪些特征是相关的，所以我们不想让惩罚分配给一个特征的系数比分配给其他任何特征的系数更大。\n"
   ],
   "metadata": {
    "origin_pos": 20
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "numeric_features = all_features.dtypes[all_features.dtypes != 'object'].index\n",
    "all_features[numeric_features] = all_features[numeric_features].apply(\n",
    "    lambda x: (x - x.mean()) / (x.std()))\n",
    "# 在标准化数据之后，所有数据都意味着消失，因此我们可以将缺失值设置为0\n",
    "all_features[numeric_features] = all_features[numeric_features].fillna(0)"
   ],
   "outputs": [],
   "metadata": {
    "origin_pos": 21,
    "tab": [
     "pytorch"
    ]
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "接下来，我们[**处理离散值。**]这包括诸如“MSZoning”之类的特征。(**我们用一次独热编码替换它们**)，方法与前面将多类别标签转换为向量的方式相同(请参见 :numref:`subsec_classification-problem` )。例如，“MSZoning”包含值“RL”和“Rm”。将创建两个新的指示器特征“MSZoning_RL”和“MSZoning_RM”，其值为0或1。根据独热编码，如果“MSZoning”的原始值为“RL”，则:“MSZoning_RL”为1，“MSZoning_RM”为0。`pandas`软件包会自动为我们实现这一点。\n"
   ],
   "metadata": {
    "origin_pos": 22
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "# `Dummy_na=True` 将“na”（缺失值）视为有效的特征值，并为其创建指示符特征。\n",
    "all_features = pd.get_dummies(all_features, dummy_na=True)\n",
    "all_features.shape"
   ],
   "outputs": [],
   "metadata": {
    "origin_pos": 23,
    "tab": [
     "pytorch"
    ]
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "你可以看到，此转换会将特征的数量从79个增加到331个。最后，通过`values`属性，我们可以[**从`pandas`格式中提取NumPy格式，并将其转换为张量表示**]用于训练。\n"
   ],
   "metadata": {
    "origin_pos": 24
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "n_train = train_data.shape[0]\n",
    "train_features = paddle.to_tensor(all_features[:n_train].values, dtype=paddle.float32)\n",
    "test_features = paddle.to_tensor(all_features[n_train:].values, dtype=paddle.float32)\n",
    "train_labels = paddle.to_tensor(\n",
    "    train_data.SalePrice.values.reshape(-1, 1), dtype=paddle.float32)"
   ],
   "outputs": [],
   "metadata": {
    "origin_pos": 25,
    "tab": [
     "pytorch"
    ]
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## [**训练**]\n",
    "\n",
    "首先，我们训练一个带有损失平方的线性模型。毫不奇怪，我们的线性模型不会让我们在竞赛中获胜，但线性模型提供了一种健全性检查，以查看数据中是否存在有意义的信息。如果我们在这里不能做得比随机猜测更好，那么我们很可能存在数据处理错误。如果一切顺利，线性模型将作为基线模型，让我们直观地知道简单的模型离报告最好的模型有多近，让我们感觉到我们应该从更酷炫的模型中获得多少收益。\n"
   ],
   "metadata": {
    "origin_pos": 26
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "loss = nn.MSELoss()\n",
    "in_features = train_features.shape[1]\n",
    "\n",
    "def get_net():\n",
    "    net = nn.Sequential(nn.Linear(in_features,1))\n",
    "    return net"
   ],
   "outputs": [],
   "metadata": {
    "origin_pos": 28,
    "tab": [
     "pytorch"
    ]
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "对于房价，就像股票价格一样，我们关心的是相对数量，而不是绝对数量。因此，[**我们更关心相对误差$\\frac{y - \\hat{y}}{y}$，**]而不是绝对误差$y - \\hat{y}$。例如，如果我们在俄亥俄州农村地区估计一栋房子的价格时，我们的预测偏差了10万美元，在那里一栋典型的房子的价值是12.5万美元，那么我们可能做得很糟糕。另一方面，如果我们在加州豪宅区的预测出现了这个数字的偏差，这可能是一个惊人的准确预测（在那里，房价均值超过400万美元）。\n",
    "\n",
    "(**解决这个问题的一种方法是用价格预测的对数来衡量差异**)。事实上，这也是比赛中官方用来评价提交质量的误差指标。即将 $\\delta$ for $|\\log y - \\log \\hat{y}| \\leq \\delta$转换为$e^{-\\delta} \\leq \\frac{\\hat{y}}{y} \\leq e^\\delta$。这使得预测价格的对数与真实标签价格的对数之间出现以下均方根误差：\n",
    "\n",
    "$$\\sqrt{\\frac{1}{n}\\sum_{i=1}^n\\left(\\log y_i -\\log \\hat{y}_i\\right)^2}.$$\n"
   ],
   "metadata": {
    "origin_pos": 30
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "def log_rmse(net, features, labels):\n",
    "    # 为了在取对数时进一步稳定该值，将小于1的值设置为1\n",
    "    clipped_preds = paddle.clip(net(features), 1, float('inf'))\n",
    "    rmse = paddle.sqrt(loss(paddle.log(clipped_preds),\n",
    "                            paddle.log(labels)))\n",
    "    return rmse.item()"
   ],
   "outputs": [],
   "metadata": {
    "origin_pos": 32,
    "tab": [
     "pytorch"
    ]
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "与前面的部分不同，[**我们的训练函数将借助Adam优化器**]（我们将在后面更详细地描述它）。这个优化器的主要吸引力在于，尽管在提供无限资源进行超参数优化方面没有做得更好（有时更差），但人们发现它对初始学习率不那么敏感。\n"
   ],
   "metadata": {
    "origin_pos": 34
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "def train(net, train_features, train_labels, test_features, test_labels,\n",
    "          num_epochs, learning_rate, weight_decay, batch_size):\n",
    "    train_ls, test_ls = [], []\n",
    "    train_iter = d2l.load_array((train_features, train_labels), batch_size)\n",
    "    # 这里使用的是Adam优化算法\n",
    "    optimizer = paddle.optimizer.Adam(learning_rate=learning_rate, parameters=net.parameters(),\n",
    "                                 weight_decay = weight_decay)\n",
    "    for epoch in range(num_epochs):\n",
    "        for X, y in train_iter:\n",
    "            l = loss(net(X), y)\n",
    "            l.backward()\n",
    "            optimizer.step()\n",
    "            optimizer.clear_grad()\n",
    "        train_ls.append(log_rmse(net, train_features, train_labels))\n",
    "        if test_labels is not None:\n",
    "            test_ls.append(log_rmse(net, test_features, test_labels))\n",
    "    return train_ls, test_ls"
   ],
   "outputs": [],
   "metadata": {
    "origin_pos": 36,
    "tab": [
     "pytorch"
    ]
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## $K$折交叉验证\n",
    "\n",
    "你可能还记得，我们在讨论模型选择的部分（ :numref:`sec_model_selection` ）中介绍了[**K折交叉验证**]。这有助于模型选择和超参数调整。我们首先需要一个函数，在$K$折交叉验证过程中返回第$i$折的数据。它选择第$i$个切片作为验证数据，其余部分作为训练数据。注意，这并不是处理数据的最有效方法，如果我们的数据集大得多，我们肯定会做一些更聪明的改变。但是这种改变所增加的复杂性可能会使代码看起来更乱。在这里可以忽略这些改变，因为我们的问题很简单。\n"
   ],
   "metadata": {
    "origin_pos": 38
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "def get_k_fold_data(k, i, X, y):\n",
    "    assert k > 1\n",
    "    fold_size = X.shape[0] // k\n",
    "    X_train, y_train = None, None\n",
    "    for j in range(k):\n",
    "        idx = slice(j * fold_size, (j + 1) * fold_size)\n",
    "        X_part, y_part = X[idx, :], y[idx]\n",
    "        if j == i:\n",
    "            X_valid, y_valid = X_part, y_part\n",
    "        elif X_train is None:\n",
    "            X_train, y_train = X_part, y_part\n",
    "        else:\n",
    "            X_train = paddle.concat([X_train, X_part], 0)\n",
    "            y_train = paddle.concat([y_train, y_part], 0)\n",
    "    return X_train, y_train, X_valid, y_valid"
   ],
   "outputs": [],
   "metadata": {
    "origin_pos": 39,
    "tab": [
     "pytorch"
    ]
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "当我们在$K$折交叉验证中训练$K$次后，[**返回训练和验证误差的平均值**]。\n"
   ],
   "metadata": {
    "origin_pos": 40
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "def k_fold(k, X_train, y_train, num_epochs, learning_rate, weight_decay,\n",
    "           batch_size):\n",
    "    train_l_sum, valid_l_sum = 0, 0\n",
    "    for i in range(k):\n",
    "        data = get_k_fold_data(k, i, X_train, y_train)\n",
    "        net = get_net()\n",
    "        train_ls, valid_ls = train(net, *data, num_epochs, learning_rate,\n",
    "                                   weight_decay, batch_size)\n",
    "        train_l_sum += train_ls[-1]\n",
    "        valid_l_sum += valid_ls[-1]\n",
    "        if i == 0:\n",
    "            d2l.plot(list(range(1, num_epochs + 1)), [train_ls, valid_ls],\n",
    "                     xlabel='epoch', ylabel='rmse', xlim=[1, num_epochs],\n",
    "                     legend=['train', 'valid'], yscale='log')\n",
    "        print(f'fold {i + 1}, train log rmse {float(train_ls[-1]):f}, '\n",
    "              f'valid log rmse {float(valid_ls[-1]):f}')\n",
    "    return train_l_sum / k, valid_l_sum / k"
   ],
   "outputs": [],
   "metadata": {
    "origin_pos": 41,
    "tab": [
     "pytorch"
    ]
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## [**模型选择**]\n",
    "\n",
    "在本例中，我们选择了一组未调优的超参数，并将其留给读者来改进模型。找到一个好的选择可能需要时间，这取决于一个人优化了多少变量。有了足够大的数据集和合理设置的超参数，$K$折交叉验证往往对多次测试具有相当的适应性。然而，如果我们尝试了不合理的大量选项，我们可能会发现验证效果不再代表真正的误差。\n"
   ],
   "metadata": {
    "origin_pos": 42
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "k, num_epochs, lr, weight_decay, batch_size = 5, 100, 5, 0, 64\n",
    "train_l, valid_l = k_fold(k, train_features, train_labels, num_epochs, lr,\n",
    "                          weight_decay, batch_size)\n",
    "print(f'{k}-折验证: 平均训练log rmse: {float(train_l):f}, '\n",
    "      f'平均验证log rmse: {float(valid_l):f}')"
   ],
   "outputs": [],
   "metadata": {
    "origin_pos": 43,
    "tab": [
     "pytorch"
    ]
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "请注意，有时一组超参数的训练误差可能非常低，但$K$折交叉验证的误差要高得多。这表明我们过拟合了。在整个训练过程中，你将希望监控训练误差和验证误差这两个数字。较少的过拟合可能表明现有数据可以支撑一个更强大的模型。较大的过拟合可能意味着我们可以通过正则化技术来获益。\n",
    "\n",
    "##  [**提交你的Kaggle预测**]\n",
    "\n",
    "既然我们知道应该选择什么样的超参数，我们不妨使用所有数据对其进行训练（而不是仅使用交叉验证中使用的$1-1/K$的数据）。然后，我们通过这种方式获得的模型可以应用于测试集。将预测保存在CSV文件中可以简化将结果上传到Kaggle的过程。\n"
   ],
   "metadata": {
    "origin_pos": 44
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "def train_and_pred(train_features, test_feature, train_labels, test_data,\n",
    "                   num_epochs, lr, weight_decay, batch_size):\n",
    "    net = get_net()\n",
    "    train_ls, _ = train(net, train_features, train_labels, None, None,\n",
    "                        num_epochs, lr, weight_decay, batch_size)\n",
    "    d2l.plot(np.arange(1, num_epochs + 1), [train_ls], xlabel='epoch',\n",
    "             ylabel='log rmse', xlim=[1, num_epochs], yscale='log')\n",
    "    print(f'train log rmse {float(train_ls[-1]):f}')\n",
    "    # 将网络应用于测试集。\n",
    "    preds = net(test_features).detach().numpy()\n",
    "    # 将其重新格式化以导出到Kaggle\n",
    "    test_data['SalePrice'] = pd.Series(preds.reshape(1, -1)[0])\n",
    "    submission = pd.concat([test_data['Id'], test_data['SalePrice']], axis=1)\n",
    "    submission.to_csv('submission.csv', index=False)"
   ],
   "outputs": [],
   "metadata": {
    "origin_pos": 45,
    "tab": [
     "pytorch"
    ]
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "一种良好的完整性检查是查看测试集上的预测是否与$K$倍交叉验证过程中的预测相似。如果是，那就是时候把它们上传到Kaggle了。下面的代码将生成一个名为`submission.csv`的文件。\n"
   ],
   "metadata": {
    "origin_pos": 46
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "train_and_pred(train_features, test_features, train_labels, test_data,\n",
    "               num_epochs, lr, weight_decay, batch_size)"
   ],
   "outputs": [],
   "metadata": {
    "origin_pos": 47,
    "tab": [
     "pytorch"
    ]
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "接下来，如 :numref:`fig_kaggle_submit2` 中所示，我们可以提交预测到Kaggle上，并查看在测试集上的预测与实际房价（标签）的比较情况。步骤非常简单：\n",
    "\n",
    "* 登录Kaggle网站，访问房价预测竞赛页面。\n",
    "* 点击“Submit Predictions”或“Late Submission”按钮(在撰写本文时，该按钮位于右侧)。\n",
    "* 点击页面底部虚线框中的“Upload Submission File”按钮，选择你要上传的预测文件。\n",
    "* 点击页面底部的“Make Submission”按钮，即可查看您的结果。\n",
    "\n",
    "![向Kaggle提交数据](../img/kaggle-submit2.png)\n",
    ":width:`400px`\n",
    ":label:`fig_kaggle_submit2`\n",
    "\n",
    "## 小结\n",
    "\n",
    "* 真实数据通常混合了不同的数据类型，需要进行预处理。\n",
    "* 将实值数据重新缩放为零均值和单位方差是一个很好的默认设置。用它们的平均值替换缺失的值也是一个很好的默认设置。\n",
    "* 将类别特征转化为指标特征，可以使我们把它们当作一个独热向量来对待。\n",
    "* 我们可以使用$K$折交叉验证来选择模型并调整超参数。\n",
    "* 对数对于相对误差很有用。\n",
    "\n",
    "## 练习\n",
    "\n",
    "1. 把你的预测提交给Kaggle。你的预测有多好？\n",
    "1. 你能通过直接最小化价格的对数来改进你的模型吗？如果你试图预测价格的对数而不是价格，会发生什么？\n",
    "1. 用平均值替换缺失值总是好主意吗？提示：你能构造一个不随机丢失值的情况吗？\n",
    "1. 通过$K$折交叉验证调整超参数，从而提高Kaggle的得分。\n",
    "1. 通过改进模型（例如，层、权重衰减和dropout）来提高分数。\n",
    "1. 如果我们没有像本节所做的那样标准化连续的数值特征，会发生什么？\n"
   ],
   "metadata": {
    "origin_pos": 48
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "[Discussions](https://discuss.d2l.ai/t/1824)\n"
   ],
   "metadata": {
    "origin_pos": 50,
    "tab": [
     "pytorch"
    ]
   }
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python",
   "version": "3.7.11",
   "mimetype": "text/x-python",
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "pygments_lexer": "ipython3",
   "nbconvert_exporter": "python",
   "file_extension": ".py"
  },
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.7.11 64-bit ('paddle': conda)"
  },
  "interpreter": {
   "hash": "02a9e8c84b0a83a507ea87c9747c54efaa8286448e4aac723db7be8f2e3f1090"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}